﻿Imports System.IO
Imports ajayumi.develop.win32.Transport.Wcf.Contracts
Imports System.ServiceModel
Imports System.ComponentModel
Imports System.Threading
Imports ajColumnHeader = ajayumi.develop.VBasic.Win.Controls.ColumnHeader
Imports ajValueType = ajayumi.develop.VBasic.Win.Controls.ValueType

Public Class frmUpload

#Region "variable"


#End Region

#Region "private method"

    ''' <summary>
    ''' 设置状态栏标签的文本值
    ''' </summary>
    ''' <param name="control">状态栏标签控件</param>
    ''' <param name="text">文本值</param>
    Private Sub setText(ByVal control As ToolStripStatusLabel, ByVal text As String)
        If Me.StatusStrip1.InvokeRequired Then
            Me.StatusStrip1.Invoke(New Action(Of ToolStripStatusLabel, String)(AddressOf Me.setText), New Object() {control, text})
        Else
            control.Text = text
        End If
    End Sub

    ''' <summary>
    ''' 初始化文件列表控件
    ''' </summary>
    Private Sub initLvFile()
        Dim _GuidCH As New ajColumnHeader(Settings.GUID_KEY, "Guid", 100, HorizontalAlignment.Left, ajValueType.Text)
        Dim _FilePathCH As New ajColumnHeader(Settings.FILEPATH_KEY, "文件路径", 200, HorizontalAlignment.Left, ajValueType.Text)
        Dim _SavePathCH As New ajColumnHeader(Settings.SAVEPATH_KEY, "保存路径", 200, HorizontalAlignment.Left, ajValueType.Text)
        Dim _FileLenghtCH As New ajColumnHeader(Settings.FILELENGTH_KEY, "文件大小", 150, HorizontalAlignment.Center, ajValueType.Number)
        Dim _ProgressCH As New ajColumnHeader(Settings.PROGRESS_KEY, "进度", 100, HorizontalAlignment.Center, ajValueType.Number)
        Dim _StautsCH As New ajColumnHeader(Settings.STAUTS_KEY, "状态", 100, HorizontalAlignment.Center, ajValueType.Text)


        Me.lvFile.Columns.AddRange(New ColumnHeader() {_GuidCH, _FilePathCH, _SavePathCH, _FileLenghtCH, _ProgressCH, _StautsCH})
    End Sub

    ''' <summary>
    ''' 创建列表项的实例
    ''' </summary>
    ''' <remarks></remarks>
    Private Function createListViewItem() As ListViewItem
        Dim _ColCount As Integer = Me.lvFile.Columns.Count - 1
        Dim _ListViewSubItems(_ColCount) As ListViewItem.ListViewSubItem
        For i As Integer = 0 To _ColCount
            _ListViewSubItems(i) = New ListViewItem.ListViewSubItem()
            _ListViewSubItems(i).Name = Me.lvFile.Columns(i).Name
        Next
        Dim _ListViewItem As New ListViewItem(_ListViewSubItems, -1)
        Return _ListViewItem
    End Function

#End Region

    Private Sub frmUpload_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.initLvFile()
    End Sub

    Private Sub tsbtnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnUpload.Click
        Try
            For Each item As ListViewItem In Me.lvFile.Items
                DirectCast(item.Tag, UploadFileTask).Start()
            Next
        Catch ex As Exception
            System.Console.WriteLine(ex.Message)
        End Try
    End Sub

    Private Sub tsbtnAddFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnAddFile.Click
        If Me.OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            For Each _FilePath As String In Me.OpenFileDialog1.FileNames
                Dim _TransFileInfo As New TransFileInfo()
                With _TransFileInfo
                    .Guid = System.Guid.NewGuid().ToString()
                    .FilePath = _FilePath
                    .SavePath = Path.Combine("C:\Temp\Receive", Path.GetFileName(_FilePath))
                End With

                Dim _ListViewItem As ListViewItem = Me.createListViewItem()
                With _ListViewItem
                    .SubItems(Settings.GUID_KEY).Text = _TransFileInfo.Guid
                    .SubItems(Settings.FILEPATH_KEY).Text = _TransFileInfo.FilePath
                    .SubItems(Settings.SAVEPATH_KEY).Text = _TransFileInfo.SavePath
                    .SubItems(Settings.FILELENGTH_KEY).Text = "0"
                    .SubItems(Settings.PROGRESS_KEY).Text = "0"
                    .SubItems(Settings.STAUTS_KEY).Text = TransportStatus.Waiting.ToString()
                End With
                
                Dim _UploadFileTask As New UploadFileTask()
                With _UploadFileTask
                    .Lv = Me.lvFile
                    .TxtMsg = Me.txtMsg
                    .TransFileInfo = _TransFileInfo
                End With
                _UploadFileTask.AddItem(_ListViewItem)
            Next
        End If
    End Sub

End Class
